/*
 * @(#)CalendarRecur.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.calendar.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.dcivision.calendar.bean.CalendarRecur;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;

/**
  CalendarRecurDAObject.java

  This class is the data access bean for table "CALENDAR_RECUR".

  @author      Tony Chen
  @company     DCIVision Limited
  @creation date   01/12/2003
  @version     $Revision: 1.9 $
*/

public class CalendarRecurDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.9 $";

  public static final String TABLE_NAME = "CALENDAR_RECUR";

  public CalendarRecurDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("CALENDAR_ID");
    this.vecDBColumn.add("START_DATE");
    this.vecDBColumn.add("END_DATE");
    this.vecDBColumn.add("REPEAT_TIMES");
    this.vecDBColumn.add("RECUR_TYPE");
    this.vecDBColumn.add("REPEAT_ON");
    this.vecDBColumn.add("REPEAT_TYPE");
    this.vecDBColumn.add("REPEAT_TYPE_DAY");
    this.vecDBColumn.add("OCCUR_TYPE");
    this.vecDBColumn.add("OCCUR_WEEK_DAY");
    this.vecDBColumn.add("OCCUR_MONTH");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.CALENDAR_ID, A.START_DATE, A.END_DATE, A.REPEAT_TIMES, A.RECUR_TYPE, A.REPEAT_ON, A.REPEAT_TYPE, A.REPEAT_TYPE_DAY, A.OCCUR_TYPE, A.OCCUR_WEEK_DAY, A.OCCUR_MONTH, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   CALENDAR_RECUR A ");
        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString());
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          CalendarRecur tmpCalendarRecur = new CalendarRecur();
          tmpCalendarRecur.setID(getResultSetInteger(rs, "ID"));
          tmpCalendarRecur.setCalendarID(getResultSetInteger(rs, "CALENDAR_ID"));
          tmpCalendarRecur.setStartDate(getResultSetTimestamp(rs, "START_DATE"));
          tmpCalendarRecur.setEndDate(getResultSetTimestamp(rs, "END_DATE"));
          tmpCalendarRecur.setRepeatTimes(getResultSetInteger(rs, "REPEAT_TIMES"));
          tmpCalendarRecur.setRecurType(getResultSetString(rs, "RECUR_TYPE"));
          tmpCalendarRecur.setRepeatOn(getResultSetInteger(rs, "REPEAT_ON"));
          tmpCalendarRecur.setRepeatType(getResultSetString(rs, "REPEAT_TYPE"));
          tmpCalendarRecur.setRepeatTypeDay(getResultSetString(rs, "REPEAT_TYPE_DAY"));
          tmpCalendarRecur.setOccurType(getResultSetInteger(rs, "OCCUR_TYPE"));
          tmpCalendarRecur.setOccurWeekDay(getResultSetInteger(rs, "OCCUR_WEEK_DAY"));
          tmpCalendarRecur.setOccurMonth(getResultSetInteger(rs, "OCCUR_MONTH"));
          tmpCalendarRecur.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpCalendarRecur.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpCalendarRecur.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpCalendarRecur.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpCalendarRecur.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpCalendarRecur.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpCalendarRecur.setCreatorName(UserInfoFactory.getUserFullName(tmpCalendarRecur.getCreatorID()));
          tmpCalendarRecur.setUpdaterName(UserInfoFactory.getUserFullName(tmpCalendarRecur.getUpdaterID()));
          return(tmpCalendarRecur);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.CALENDAR_ID, A.START_DATE, A.END_DATE, A.REPEAT_TIMES, A.RECUR_TYPE, A.REPEAT_ON, A.REPEAT_TYPE, A.REPEAT_TYPE_DAY, A.OCCUR_TYPE, A.OCCUR_WEEK_DAY, A.OCCUR_MONTH, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   CALENDAR_RECUR A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");

        // Form the WHERE clause for filtering.
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          CalendarRecur tmpCalendarRecur = new CalendarRecur();
          tmpCalendarRecur.setID(getResultSetInteger(rs, "ID"));
          tmpCalendarRecur.setCalendarID(getResultSetInteger(rs, "CALENDAR_ID"));
          tmpCalendarRecur.setStartDate(getResultSetTimestamp(rs, "START_DATE"));
          tmpCalendarRecur.setEndDate(getResultSetTimestamp(rs, "END_DATE"));
          tmpCalendarRecur.setRepeatTimes(getResultSetInteger(rs, "REPEAT_TIMES"));
          tmpCalendarRecur.setRecurType(getResultSetString(rs, "RECUR_TYPE"));
          tmpCalendarRecur.setRepeatOn(getResultSetInteger(rs, "REPEAT_ON"));
          tmpCalendarRecur.setRepeatType(getResultSetString(rs, "REPEAT_TYPE"));
          tmpCalendarRecur.setRepeatTypeDay(getResultSetString(rs, "REPEAT_TYPE_DAY"));
          tmpCalendarRecur.setOccurType(getResultSetInteger(rs, "OCCUR_TYPE"));
          tmpCalendarRecur.setOccurWeekDay(getResultSetInteger(rs, "OCCUR_WEEK_DAY"));
          tmpCalendarRecur.setOccurMonth(getResultSetInteger(rs, "OCCUR_MONTH"));
          tmpCalendarRecur.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpCalendarRecur.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpCalendarRecur.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpCalendarRecur.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpCalendarRecur.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpCalendarRecur.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpCalendarRecur.setCreatorName(UserInfoFactory.getUserFullName(tmpCalendarRecur.getCreatorID()));
          tmpCalendarRecur.setUpdaterName(UserInfoFactory.getUserFullName(tmpCalendarRecur.getUpdaterID()));
          tmpCalendarRecur.setRecordCount(totalNumOfRecord);
          tmpCalendarRecur.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpCalendarRecur);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.CALENDAR_ID, A.START_DATE, A.END_DATE, A.REPEAT_TIMES, A.RECUR_TYPE, A.REPEAT_ON, A.REPEAT_TYPE, A.REPEAT_TYPE_DAY, A.OCCUR_TYPE, A.OCCUR_WEEK_DAY, A.OCCUR_MONTH, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   CALENDAR_RECUR A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString());
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          CalendarRecur tmpCalendarRecur = new CalendarRecur();
          tmpCalendarRecur.setID(getResultSetInteger(rs, "ID"));
          tmpCalendarRecur.setCalendarID(getResultSetInteger(rs, "CALENDAR_ID"));
          tmpCalendarRecur.setStartDate(getResultSetTimestamp(rs, "START_DATE"));
          tmpCalendarRecur.setEndDate(getResultSetTimestamp(rs, "END_DATE"));
          tmpCalendarRecur.setRepeatTimes(getResultSetInteger(rs, "REPEAT_TIMES"));
          tmpCalendarRecur.setRecurType(getResultSetString(rs, "RECUR_TYPE"));
          tmpCalendarRecur.setRepeatOn(getResultSetInteger(rs, "REPEAT_ON"));
          tmpCalendarRecur.setRepeatType(getResultSetString(rs, "REPEAT_TYPE"));
          tmpCalendarRecur.setRepeatTypeDay(getResultSetString(rs, "REPEAT_TYPE_DAY"));
          tmpCalendarRecur.setOccurType(getResultSetInteger(rs, "OCCUR_TYPE"));
          tmpCalendarRecur.setOccurWeekDay(getResultSetInteger(rs, "OCCUR_WEEK_DAY"));
          tmpCalendarRecur.setOccurMonth(getResultSetInteger(rs, "OCCUR_MONTH"));
          tmpCalendarRecur.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpCalendarRecur.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpCalendarRecur.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpCalendarRecur.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpCalendarRecur.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpCalendarRecur.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpCalendarRecur.setCreatorName(UserInfoFactory.getUserFullName(tmpCalendarRecur.getCreatorID()));
          tmpCalendarRecur.setUpdaterName(UserInfoFactory.getUserFullName(tmpCalendarRecur.getUpdaterID()));
          result.add(tmpCalendarRecur);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    CalendarRecur tmpCalendarRecur = (CalendarRecur)((CalendarRecur)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   CALENDAR_RECUR(ID, CALENDAR_ID, START_DATE, END_DATE, REPEAT_TIMES, RECUR_TYPE, REPEAT_ON, REPEAT_TYPE, REPEAT_TYPE_DAY, OCCUR_TYPE, OCCUR_WEEK_DAY, OCCUR_MONTH, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString());
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpCalendarRecur.getCalendarID());
        setPrepareStatement(preStat, 3, tmpCalendarRecur.getStartDate());
        setPrepareStatement(preStat, 4, tmpCalendarRecur.getEndDate());
        setPrepareStatement(preStat, 5, tmpCalendarRecur.getRepeatTimes());
        setPrepareStatement(preStat, 6, tmpCalendarRecur.getRecurType());
        setPrepareStatement(preStat, 7, tmpCalendarRecur.getRepeatOn());
        setPrepareStatement(preStat, 8, tmpCalendarRecur.getRepeatType());
        setPrepareStatement(preStat, 9, tmpCalendarRecur.getRepeatTypeDay());
        setPrepareStatement(preStat, 10, tmpCalendarRecur.getOccurType());
        setPrepareStatement(preStat, 11, tmpCalendarRecur.getOccurWeekDay());
        setPrepareStatement(preStat, 12, tmpCalendarRecur.getOccurMonth());
        setPrepareStatement(preStat, 13, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 14, new Integer(0));
        setPrepareStatement(preStat, 15, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 16, currTime);
        setPrepareStatement(preStat, 17, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 18, currTime);
        preStat.executeUpdate();
        tmpCalendarRecur.setID(nextID);
        tmpCalendarRecur.setCreatorID(sessionContainer.getUserRecordID());
        tmpCalendarRecur.setCreateDate(currTime);
        tmpCalendarRecur.setUpdaterID(sessionContainer.getUserRecordID());
        tmpCalendarRecur.setUpdateDate(currTime);
        tmpCalendarRecur.setUpdateCount(new Integer(0));
        tmpCalendarRecur.setCreatorName(UserInfoFactory.getUserFullName(tmpCalendarRecur.getCreatorID()));
        tmpCalendarRecur.setUpdaterName(UserInfoFactory.getUserFullName(tmpCalendarRecur.getUpdaterID()));
        return(tmpCalendarRecur);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    CalendarRecur tmpCalendarRecur = (CalendarRecur)((CalendarRecur)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE CALENDAR_RECUR ");
        sqlStat.append("SET  CALENDAR_ID=?, START_DATE=?, END_DATE=?, REPEAT_TIMES=?, RECUR_TYPE=?, REPEAT_ON=?, REPEAT_TYPE=?, REPEAT_TYPE_DAY=?, OCCUR_TYPE=?, OCCUR_WEEK_DAY=?, OCCUR_MONTH=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString());
        setPrepareStatement(preStat, 1, tmpCalendarRecur.getCalendarID());
        setPrepareStatement(preStat, 2, tmpCalendarRecur.getStartDate());
        setPrepareStatement(preStat, 3, tmpCalendarRecur.getEndDate());
        setPrepareStatement(preStat, 4, tmpCalendarRecur.getRepeatTimes());
        setPrepareStatement(preStat, 5, tmpCalendarRecur.getRecurType());
        setPrepareStatement(preStat, 6, tmpCalendarRecur.getRepeatOn());
        setPrepareStatement(preStat, 7, tmpCalendarRecur.getRepeatType());
        setPrepareStatement(preStat, 8, tmpCalendarRecur.getRepeatTypeDay());
        setPrepareStatement(preStat, 9, tmpCalendarRecur.getOccurType());
        setPrepareStatement(preStat, 10, tmpCalendarRecur.getOccurWeekDay());
        setPrepareStatement(preStat, 11, tmpCalendarRecur.getOccurMonth());
        setPrepareStatement(preStat, 12, new Integer(tmpCalendarRecur.getUpdateCount().intValue() + 1));
        setPrepareStatement(preStat, 13, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 14, currTime);
        setPrepareStatement(preStat, 15, tmpCalendarRecur.getID());
        setPrepareStatement(preStat, 16, tmpCalendarRecur.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpCalendarRecur.setUpdaterID(sessionContainer.getUserRecordID());
          tmpCalendarRecur.setUpdateDate(currTime);
          tmpCalendarRecur.setUpdateCount(new Integer(tmpCalendarRecur.getUpdateCount().intValue() + 1));
          tmpCalendarRecur.setCreatorName(UserInfoFactory.getUserFullName(tmpCalendarRecur.getCreatorID()));
          tmpCalendarRecur.setUpdaterName(UserInfoFactory.getUserFullName(tmpCalendarRecur.getUpdaterID()));
          return(tmpCalendarRecur);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    CalendarRecur tmpCalendarRecur = (CalendarRecur)((CalendarRecur)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   CALENDAR_RECUR ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString());
        setPrepareStatement(preStat, 1, tmpCalendarRecur.getID());
        setPrepareStatement(preStat, 2, tmpCalendarRecur.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpCalendarRecur);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    CalendarRecur tmpCalendarRecur = (CalendarRecur)this.oldValue;

    if (tmpCalendarRecur != null) {
      oldValues.add(toAuditTrailValue(tmpCalendarRecur.getID()));
      oldValues.add(toAuditTrailValue(tmpCalendarRecur.getCalendarID()));
      oldValues.add(toAuditTrailValue(tmpCalendarRecur.getStartDate()));
      oldValues.add(toAuditTrailValue(tmpCalendarRecur.getEndDate()));
      oldValues.add(toAuditTrailValue(tmpCalendarRecur.getRepeatTimes()));
      oldValues.add(toAuditTrailValue(tmpCalendarRecur.getRecurType()));
      oldValues.add(toAuditTrailValue(tmpCalendarRecur.getRepeatOn()));
      oldValues.add(toAuditTrailValue(tmpCalendarRecur.getRepeatType()));
      oldValues.add(toAuditTrailValue(tmpCalendarRecur.getRepeatTypeDay()));
      oldValues.add(toAuditTrailValue(tmpCalendarRecur.getOccurType()));
      oldValues.add(toAuditTrailValue(tmpCalendarRecur.getOccurWeekDay()));
      oldValues.add(toAuditTrailValue(tmpCalendarRecur.getOccurMonth()));
      oldValues.add(toAuditTrailValue(tmpCalendarRecur.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpCalendarRecur.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpCalendarRecur.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpCalendarRecur.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpCalendarRecur.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpCalendarRecur.getUpdateDate()));
    }

    tmpCalendarRecur = (CalendarRecur)obj;
    if (tmpCalendarRecur != null) {
      newValues.add(toAuditTrailValue(tmpCalendarRecur.getID()));
      newValues.add(toAuditTrailValue(tmpCalendarRecur.getCalendarID()));
      newValues.add(toAuditTrailValue(tmpCalendarRecur.getStartDate()));
      newValues.add(toAuditTrailValue(tmpCalendarRecur.getEndDate()));
      newValues.add(toAuditTrailValue(tmpCalendarRecur.getRepeatTimes()));
      newValues.add(toAuditTrailValue(tmpCalendarRecur.getRecurType()));
      newValues.add(toAuditTrailValue(tmpCalendarRecur.getRepeatOn()));
      newValues.add(toAuditTrailValue(tmpCalendarRecur.getRepeatType()));
      newValues.add(toAuditTrailValue(tmpCalendarRecur.getRepeatTypeDay()));
      newValues.add(toAuditTrailValue(tmpCalendarRecur.getOccurType()));
      newValues.add(toAuditTrailValue(tmpCalendarRecur.getOccurWeekDay()));
      newValues.add(toAuditTrailValue(tmpCalendarRecur.getOccurMonth()));
      newValues.add(toAuditTrailValue(tmpCalendarRecur.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpCalendarRecur.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpCalendarRecur.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpCalendarRecur.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpCalendarRecur.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpCalendarRecur.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/
  public synchronized AbstractBaseObject getObjectByCalendarID(Integer calendarID) throws ApplicationException {
        PreparedStatement preStat = null;
        ResultSet rs = null;
        StringBuffer sqlStat = new StringBuffer();

        synchronized(dbConn) {
            try {
                sqlStat.append("SELECT ID, CALENDAR_ID, START_DATE, END_DATE, REPEAT_TIMES, RECUR_TYPE, REPEAT_ON, REPEAT_TYPE, REPEAT_TYPE_DAY, OCCUR_TYPE, OCCUR_WEEK_DAY, OCCUR_MONTH, RECORD_STATUS, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE ");
                sqlStat.append("FROM   CALENDAR_RECUR ");
                sqlStat.append("WHERE  CALENDAR_ID = ? ");
                preStat = dbConn.prepareStatement(sqlStat.toString());
                this.setPrepareStatement(preStat, 1, calendarID);
                rs = preStat.executeQuery();
                if (rs.next()) {
                    CalendarRecur tmpCalendarRecur = new CalendarRecur();
                    tmpCalendarRecur.setID(getResultSetInteger(rs, "ID"));
                    tmpCalendarRecur.setCalendarID(getResultSetInteger(rs, "CALENDAR_ID"));
                    tmpCalendarRecur.setStartDate(getResultSetTimestamp(rs, "START_DATE"));
                    tmpCalendarRecur.setEndDate(getResultSetTimestamp(rs, "END_DATE"));
                    tmpCalendarRecur.setRepeatTimes(getResultSetInteger(rs, "REPEAT_TIMES"));
                    tmpCalendarRecur.setRecurType(getResultSetString(rs, "RECUR_TYPE"));
                    tmpCalendarRecur.setRepeatOn(getResultSetInteger(rs, "REPEAT_ON"));
                    tmpCalendarRecur.setRepeatType(getResultSetString(rs, "REPEAT_TYPE"));
                    tmpCalendarRecur.setRepeatTypeDay(getResultSetString(rs, "REPEAT_TYPE_DAY"));
                    tmpCalendarRecur.setOccurType(getResultSetInteger(rs, "OCCUR_TYPE"));
                    tmpCalendarRecur.setOccurWeekDay(getResultSetInteger(rs, "OCCUR_WEEK_DAY"));
                    tmpCalendarRecur.setOccurMonth(getResultSetInteger(rs, "OCCUR_MONTH"));
                    tmpCalendarRecur.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
                    tmpCalendarRecur.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
                    tmpCalendarRecur.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
                    tmpCalendarRecur.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
                    tmpCalendarRecur.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
                    tmpCalendarRecur.setCreatorName(sessionContainer == null ? "" : sessionContainer.getUserRecord().getCreatorName());
                    tmpCalendarRecur.setUpdaterName(sessionContainer == null ? "" : sessionContainer.getUserRecord().getUpdaterName());
                    return(tmpCalendarRecur);
                } else {
                    //throw new ApplicationException("common.E00004");
                  return null;
                }
            } catch (ApplicationException appEx) {
                throw appEx;
            } catch (SQLException sqle) {
                log.error(sqle, sqle);
                throw new ApplicationException("common.E00001", sqle);
            } catch (Exception e) {
                log.error(e, e);
                throw new ApplicationException("common.E00009", e);
            } finally {
                try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
                try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
            }
        }
   }

   public synchronized void deleteByCalendarID(Integer calID) throws ApplicationException {
        PreparedStatement preStat = null;
        StringBuffer sqlStat = new StringBuffer();
        //CalendarRecur tmpCalendarRecur = (CalendarRecur)((CalendarRecur)obj).clone();

        synchronized(dbConn) {
            try {
                int updateCnt = 0;
                sqlStat.append("DELETE ");
                sqlStat.append("FROM   CALENDAR_RECUR ");
                sqlStat.append("WHERE  CALENDAR_ID=?  ");
                preStat = dbConn.prepareStatement(sqlStat.toString());
                setPrepareStatement(preStat, 1, calID);

                updateCnt = preStat.executeUpdate();
/*                 if (updateCnt == 0) {
                    throw new ApplicationException("common.E00005");
                } else {
                    return(null);
                }
                */
//            } catch (ApplicationException appEx) {
//                throw appEx;
            } catch (SQLException sqle) {
                log.error(sqle, sqle);
                throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle);
            } catch (Exception e) {
                log.error(e, e);
                throw new ApplicationException(ErrorConstant.COMMON_FATAL_ERROR, e);
            } finally {
                try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
            }
        }
   }


}
